package com.forum.module.blog.controller.notification;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.module.blog.controller.article.vo.ArticleRespVO;
import com.forum.module.blog.controller.likes.vo.LikesPageReqVO;
import com.forum.module.blog.controller.likes.vo.LikesRespVO;
import com.forum.module.blog.controller.likes.vo.LikesSavaReqVO;
import com.forum.module.blog.controller.notification.vo.NotificationPageReqVO;
import com.forum.module.blog.controller.notification.vo.NotificationRespVO;
import com.forum.module.blog.controller.notification.vo.NotificationSavaReqVO;
import com.forum.module.blog.convert.likes.LikesConvert;
import com.forum.module.blog.convert.notification.NotificationConvert;
import com.forum.module.blog.dal.dataobject.article.ArticleDO;
import com.forum.module.blog.dal.dataobject.likes.LikesDO;
import com.forum.module.blog.dal.dataobject.notification.NotificationDO;
import com.forum.module.blog.service.likes.LikesService;
import com.forum.module.blog.service.notification.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/4 19:18
 */
@Tag(name = "博客管理 - 站内信")
@RestController
@RequestMapping("/blog/notification")
public class NotificationController {
    private final NotificationService notificationService;

    public NotificationController(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增站内信")
    public DataResult<Long> createNotification(@RequestBody NotificationSavaReqVO reqVO) {
        Long id = notificationService.createNotification(reqVO);
        return success(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新站内信")
    public DataResult<Boolean> updateNotification(@RequestBody NotificationSavaReqVO reqVO) {
        notificationService.updateNotification(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站内信信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<Boolean> deleteNotification(@RequestParam("id") Long id) {
        notificationService.deleteById(id);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询当前登录用户站内信列表")
    public DataResult<PageResult<NotificationRespVO>> page(@RequestBody @Valid NotificationPageReqVO pageReqVO) {
        // 获得当前登录用户站内信分页列表
        PageResult<NotificationDO> pageResult = notificationService.getNotificationPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

        return success(new PageResult<>(NotificationConvert.INSTANCE.covertList(pageResult.getList()), pageResult.getTotal()));
    }

    @GetMapping("/get")
    @Operation(summary = "用户站内信详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<NotificationRespVO> getNotification(@RequestParam("id") Long id) {
        NotificationDO notificationDO = notificationService.getNotification(id);
        if (notificationDO == null) {
            return success(null);
        }

        return success(NotificationConvert.INSTANCE.covert(notificationDO));
    }
}
